package com.yanqu.road.dao.impl.activity.oldplayerreturn;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.oldplayerreturn.OldPlayerReturnDayLoginTaskConfig;
import com.yanqu.road.entity.activity.oldplayerreturn.OldPlayerReturnEventConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/12/6 14:46
 */
public class OldPlayerReturnConfigDaoImpl extends TempDao {
    public static String getEventConfigSql = "select * from t_s_activity_event where activityId= ?";
    public Map<Integer, OldPlayerReturnEventConfig> getEventConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, OldPlayerReturnEventConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getEventConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                int eventId = rs.getInt("eventId");
                int npcId = rs.getInt("npcId");
                String name = rs.getString("name");
                String npcName = rs.getString("npcName");
                String desc = rs.getString("desc");
                String reward = rs.getString("reward");
                int weight = rs.getInt("weight");
                String placeIdList = rs.getString("placeIdList");
                OldPlayerReturnEventConfig data =
                        new OldPlayerReturnEventConfig(
                                activityId,
                                eventId,
                                npcId,
                                name,
                                npcName,
                                desc,
                                reward,
                                weight,
                                placeIdList);
                result.put(data.getEventId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static String getOldPlayerReturnDayLoginTaskConfigSql = "select * from `t_s_activity_return_find` where `activityId`=?";
    public Map<Integer,List<OldPlayerReturnDayLoginTaskConfig>> getOldPlayerReturnDayLoginTaskConfigList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,List<OldPlayerReturnDayLoginTaskConfig>> configMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getOldPlayerReturnDayLoginTaskConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                OldPlayerReturnDayLoginTaskConfig oldPlayerReturnDayLoginTaskConfig = new OldPlayerReturnDayLoginTaskConfig();
                oldPlayerReturnDayLoginTaskConfig.setActivityId(rs.getInt("activityId"));
                oldPlayerReturnDayLoginTaskConfig.setId(rs.getInt("id"));
                oldPlayerReturnDayLoginTaskConfig.setDay(rs.getInt("day"));
                oldPlayerReturnDayLoginTaskConfig.setItemId(rs.getInt("itemId"));
                oldPlayerReturnDayLoginTaskConfig.setBaseNum(rs.getInt("baseNum"));
                oldPlayerReturnDayLoginTaskConfig.setVipParameter(rs.getString("vipParameter"));
                if (configMap.get(oldPlayerReturnDayLoginTaskConfig.getDay())==null){
                    configMap.put(oldPlayerReturnDayLoginTaskConfig.getDay(),new ArrayList<>());
                }
                configMap.get(oldPlayerReturnDayLoginTaskConfig.getDay()).add(oldPlayerReturnDayLoginTaskConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }
}
